package cn.com.nes.site.controller.crm.sale.vo;

import cn.com.nes.site.controller.crm.saleorder.vo.SaleDetailVO;
import cn.com.nes.site.controller.crm.saleorder.vo.SaleMasterVO;
import cn.com.nes.site.entity.em.crm.customer.CustomerNatureEnum;
import cn.com.nes.site.entity.em.crm.customer.CustomerSaveTypeEnum;
import cn.com.nes.site.entity.em.crm.customer.EnergyTypeEnum;
import cn.com.nes.site.entity.em.crm.opportunity.DeclareCategoryEnum;
import cn.com.nes.site.entity.em.crm.opportunity.OpportunityClassificationEnum;
import cn.com.nes.site.entity.em.crm.opportunity.OpportunityStageEnum;
import cn.com.nes.site.entity.em.crm.opportunity.OpportunityTypeEnum;
import cn.com.nes.site.entity.em.crm.saleorder.SaleOrderPayModelEnum;
import cn.com.nes.site.entity.em.crm.saleorder.SaleOrderStatusEnum;
import cn.com.nes.site.entity.em.crm.saleorder.SaleOrderTypeEnum;
import cn.com.nes.site.entity.em.project.ProjectRelationDataTypeEnum;
import cn.com.nes.site.entity.vo.BaseVO;
import cn.com.nes.site.modules.sale.bo.SaleBO;
import cn.com.nes.site.modules.sale.enums.SaleTypeEnum;
import cn.com.nes.site.service.crm.customer.bo.CustomerBO;
import cn.com.nes.site.service.crm.opportunity.bo.OpportunityBO;
import cn.com.nes.site.service.crm.order.bo.SaleDetailBO;
import cn.com.nes.site.service.crm.order.bo.SaleMasterBO;
import cn.com.nes.site.service.crm.order.bo.SaleOrderBO;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@ApiModel(value = "政府销售入参模型")
@Data
public class GovernmentSaleVO extends BaseVO {

    @ApiModelProperty(value = "保存来源", notes = "01-商务项目，02-施工项目")
    private String saveSoure = "01";

    @ApiModelProperty(value = "主商机UUID", required = true)
    @NotEmpty(message = "主商机UUID")
    private String mainOpportunityUuid;

    @ApiModelProperty(value = "行政区划", required = true)
    @NotEmpty(message = "行政区划")
    private String areaCode;

    @ApiModelProperty(value = "手机号", required = true)
    @NotEmpty(message = "手机号")
    private String phone;

    @ApiModelProperty(value = "门牌号", required = true)
    private String houseNumber;

    @ApiModelProperty(value = "客户名称", required = true)
    @NotEmpty(message = "客户名称")
    private String customerName;

    @ApiModelProperty(value = "身份证号", required = true)
    @NotEmpty(message = "身份证号")
    private String idCardNumber;

    @ApiModelProperty(value = "地址", required = true)
    @NotEmpty(message = "地址")
    private String address;

    @ApiModelProperty(value = "住宅面积", required = true)
    @NotNull(message = "住宅面积")
    private BigDecimal houseArea;

    @ApiModelProperty(value = "建筑结构", required = true)
    private String buildingStructure;

    @ApiModelProperty(value = "房屋类型", required = true)
    private Integer buildingType;

    @ApiModelProperty(value = "采暖末端", required = true)
    private Integer heatingEnd;

    @ApiModelProperty(value = "计划年度", required = true)
    private String planYear;

    @ApiModelProperty(value = "能源类型：0-煤改电，1-煤改气，2-生物质，3-清洁煤", required = true)
    @NotNull(message = "能源类型")
    private Integer energyType;

    @ApiModelProperty(value = "供暖方式")
    private Integer heatingMode;

    @ApiModelProperty("实际成交时间")
    private Date realTradingTime;

    @ApiModelProperty(value = "预计安装时间")
    private Date estimatedInstallTime;

    @ApiModelProperty(value = "系统单", required = true)
    @NotEmpty(message = "系统单")
    private List<SaleMasterVO> saleMasters;
    @NotNull(message = "身份证正面照片")
    @ApiModelProperty("身份证正面照片")
    private String idCardFrontImage;
    @NotNull(message = "身份证反面照片")
    @ApiModelProperty("身份证反面照片")
    private String idCardBackImage;
    @NotNull(message = "户口本首页照片")
    @ApiModelProperty("户口本首页照片")
    private String householdRegistrationBookFirstPageImage;
    @NotNull(message = "户口本户主页照片")
    @ApiModelProperty("户口本户主页照片")
    private String householdRegistrationBookHomePageImage;
    @ApiModelProperty("电费发票照片")
    private String electricityInvoiceImage;

    @ApiModelProperty("长期居住证明照片")
    private String longTermResidenceCertificateImage;

    @ApiModelProperty("未享受补贴证明照片")
    private String notEnjoySubsidiesCertificateImage;

    @ApiModelProperty("特殊情况证明照片")
    private String specialCircumstancesCertificateImage;

    @ApiModelProperty(value = "是否农村")
    private String isVillage;

    @ApiModelProperty(value = "采暖面积")
    private BigDecimal heatingArea;

    @ApiModelProperty(value = "常用采暖面积")
    private BigDecimal commonHeatingArea;

    @ApiModelProperty("新申报面积")
    private BigDecimal newlyDeclaredArea;

    @ApiModelProperty("原申报面积")
    private BigDecimal originalDeclaredArea;

    @ApiModelProperty(value = "建筑层高")
    private BigDecimal buildingHeight;

    @ApiModelProperty(value = "外墙厚度")
    private Integer outerWallThickness;

    @ApiModelProperty(value = "外墙保温")
    private String outerWallKeepWarm;

    @ApiModelProperty(value = "安装位置")
    private Integer installPosition;

    @ApiModelProperty(value = "房屋用途")
    private Integer housePerpose;

    @ApiModelProperty(value = "房屋间数")
    private BigDecimal roomCount;

    @ApiModelProperty(value = "窗户类型")
    private Integer windowType;

    @ApiModelProperty(value = "房屋朝向")
    private Integer houseDirection;

    @ApiModelProperty(value = "末端状态")
    private Integer endState;

    @ApiModelProperty(value = "表号", notes = "根据能源类型判断输入的是电表号还是燃气表号，除电/燃气此字段有值，其他类别没值")
    private String meterNumber;

    @ApiModelProperty(value = "用能主体性质")
    private String energyUsePropertyCode;

    @ApiModelProperty(value = "客户编号")
    private String customerCode;

    @ApiModelProperty("是否安装光伏")
    private Boolean installPhotovoltaic = false;

    @ApiModelProperty("设计单位")
    private String designCompany;

    @ApiModelProperty("设计热负荷")
    private BigDecimal designDirection;

    @ApiModelProperty("户籍情况")
    private String localHouseHold;

    @ApiModelProperty("用能主体性质")
    private String natureEnergyType;

    @ApiModelProperty("图纸编号")
    private String paperCode;

    @ApiModelProperty(value = "是否本村户籍")
    private String isVillagers;

    @NotNull(message = "申请种类")
    @ApiModelProperty(value = "申请种类")
    private String applicationType;

    @ApiModelProperty(value = "低保户或五保户")
    private String dbhhwbh;

    @ApiModelProperty(value = "设计图")
    private List<String> designDrawings;

    @ApiModelProperty(value = "平面图")
    private List<String> planeGraphs;

    @NotNull(message = "设备告知承诺书")
    @ApiModelProperty(value = "设备告知承诺书")
    private List<String> sbgzcns;

    @ApiModelProperty(value = "旧设备与户主合影")
    @NotNull(message = "旧设备与户主合影")
    private String jsbyhzhy;

    @ApiModelProperty(value = "低保户或五保户资格证明")
    private String dbhhwbhzm;
    @ApiModelProperty(value = "客户名称")
    @NotNull(message = "客户名称")
    private String customerNameNow;
    @ApiModelProperty(value = "客户电话")
    @NotNull(message = "客户电话")
    private String  phoneNow;
    @ApiModelProperty(value = "是否户主")
    @NotNull(message = "是否户主")
    private String isHouseholder;
    @ApiModelProperty(value = "客户身份证号")
    @NotNull(message = "客户身份证号")
    private String idCardNumberNow;
    @ApiModelProperty(value = "联系人id")
    private String customerStaffId;
    public SaleBO buildSaleBO() {
        SaleBO saleBO = new SaleBO();
        saleBO.setSaleType(SaleTypeEnum.GOVERNMENT_PERSONAL_SALES);

        // 构建 客户BO
        saleBO.setCustomerBO(buildCustomerBO());

        // 构建 商机BO
        saleBO.setOpptunityBO(buildOpportunityBO());

        // 构建 销售单BO
        saleBO.setSaleOrderBO(buildSaleOrderBO());

        return saleBO;
    }

    private SaleOrderBO buildSaleOrderBO() {
        SaleOrderBO saleOrderBO = new SaleOrderBO();
        saleOrderBO.setAddOrder(Boolean.TRUE);
        saleOrderBO.setHandleForExcelImport(Boolean.TRUE);
        BeanUtils.copyProperties(this, saleOrderBO);
        saleOrderBO.setSaleOrderName(customerName + " - 政府终端用户订单");
        saleOrderBO.setAreaCode(areaCode);
        saleOrderBO.setPhone(phone);
        saleOrderBO.setInstallAddress(address);
        saleOrderBO.setEstimatedInstallTime(estimatedInstallTime);
        saleOrderBO.setRealTradingTime(realTradingTime);
        saleOrderBO.setCompanyId(getOperateUserCompanyId());
        saleOrderBO.setRealTradingTime(getOperateDate());
        saleOrderBO.setPayModel(SaleOrderPayModelEnum.PAIED_AFTER_INSTALL);
        saleOrderBO.setSaleOrderStatus("01".equals(this.getSaveSoure()) ? SaleOrderStatusEnum.TO_BE_PAID: SaleOrderStatusEnum.PAID_SUCCESS);
        saleOrderBO.setOrderType(SaleOrderTypeEnum.PERSONAL);
        saleOrderBO.setCommit(Boolean.TRUE);
        saleOrderBO.setDiscount(100);

        BigDecimal totalPrice = BigDecimal.ZERO;
        List<SaleMasterBO>  saleMasterBOList = new ArrayList<>();
        for (SaleMasterVO saleMasterVO : saleMasters) {
            BigDecimal totalAmount = BigDecimal.ZERO;
            SaleMasterBO saleMasterBO = new SaleMasterBO();
            BeanUtils.copyProperties(saleOrderBO, saleMasterBO);
            saleMasterBO.setMasterId(saleMasterVO.getId());
            saleMasterBO.setSaleMasterName(StrUtil.isNotEmpty(saleMasterVO.getName()) ? saleMasterVO.getName() : "供热系统");
            saleMasterBO.setCompanyId(saleOrderBO.getCompanyId());
            List<SaleDetailBO> saleDetailBOList = new ArrayList<>();
            for (SaleDetailVO saleDetailVO : saleMasterVO.getChildList()) {
                SaleDetailBO saleDetailBO = new SaleDetailBO();
                BeanUtils.copyProperties(saleOrderBO, saleDetailBO);
                saleDetailBO.setBrandId(saleDetailVO.getBrandId());
                saleDetailBO.setEquipmentId(saleDetailVO.getEquipmentId());
                saleDetailBO.setEquipmentName(saleDetailVO.getEquipmentName());
                saleDetailBO.setGuidePrice(saleDetailVO.getGuidePrice());
                saleDetailBO.setPrice(saleDetailVO.getTotalPrice());
                saleDetailBO.setCount(saleDetailVO.getCount());
                saleDetailBO.setDiscount(100);
                saleDetailBO.setUnit(saleDetailVO.getUnit());
                saleDetailBO.setCompanyId(saleMasterBO.getCompanyId());
                saleDetailBO.setTotalPrice(saleDetailVO.getTotalPrice());
                saleDetailBOList.add(saleDetailBO);
                totalAmount = totalAmount.add(saleDetailVO.getTotalPrice());

            }
            saleMasterBO.setDiscount(100);
            saleMasterBO.setTotalPrice(totalAmount);
            saleMasterBO.setTotalOffer(totalAmount);
            saleMasterBO.setSaleDetails(saleDetailBOList);
            saleMasterBOList.add(saleMasterBO);
            totalPrice = totalPrice.add(totalAmount);
        }
        saleOrderBO.setTotalPrice(totalPrice);
        saleOrderBO.setTotalOffer(totalPrice);
        saleOrderBO.setSaleMasters(saleMasterBOList);

        return saleOrderBO;
    }

    private OpportunityBO buildOpportunityBO() {
        OpportunityBO opportunityBO = new OpportunityBO();
        BeanUtils.copyProperties(this, opportunityBO);
        opportunityBO.setProjectRelationDataTypeEnum(ProjectRelationDataTypeEnum.MANUAL_OPERATION);
        opportunityBO.setRelateOpportunityUuid(mainOpportunityUuid);
        opportunityBO.setAddOpportunity(Boolean.TRUE);
        opportunityBO.setOpportunityName(customerName + " - 政府终端用户商机");
        opportunityBO.setPersonInCharge(customerName);
        opportunityBO.setPersonInChargePhone(phone);
        opportunityBO.setOpportunityClassification(OpportunityClassificationEnum.END_USRE);
        opportunityBO.setOpportunityStage(OpportunityStageEnum._transaction);
        opportunityBO.setOpportunityType(OpportunityTypeEnum._purchase);
        opportunityBO.setProjectAddress(address);
        opportunityBO.setAreaCode(areaCode);
        opportunityBO.setProjectArea(houseArea);
        opportunityBO.setNewlyDeclaredArea(newlyDeclaredArea);
        opportunityBO.setCategoryOfDeclaration(DeclareCategoryEnum.NEW);
        opportunityBO.setDesignHeatingLoad(designDirection);
        opportunityBO.setEndState(String.valueOf(endState));
        opportunityBO.setEnergyType(EnergyTypeEnum.getEnum(String.valueOf(energyType)));
        opportunityBO.setHeatingEnd(String.valueOf(heatingEnd));
        opportunityBO.setHeatingMode(String.valueOf(heatingMode));
        opportunityBO.setHouseDirection(String.valueOf(houseDirection));
        opportunityBO.setHousePerpose(String.valueOf(housePerpose));
        opportunityBO.setInstallPosition(String.valueOf(installPosition));
        opportunityBO.setHouseholdRegistration(localHouseHold);
        opportunityBO.setElectricityMeterNumbers(customerCode + "-" + meterNumber);
        opportunityBO.setOuterWallThickness(String.valueOf(outerWallThickness));
        opportunityBO.setDrawingNo(paperCode);
        opportunityBO.setRoomCount(roomCount);
        opportunityBO.setWindowType(String.valueOf(windowType));
        opportunityBO.setApplicationType(applicationType);
        opportunityBO.setDbhhwbh(dbhhwbh);
        return opportunityBO;
    }

    private CustomerBO buildCustomerBO() {
        CustomerBO customerBO = new CustomerBO();
        BeanUtils.copyProperties(this, customerBO);
        customerBO.setContact(customerName);
        customerBO.setXzqhdm(areaCode);
        customerBO.setSaveType(CustomerSaveTypeEnum._my);
        customerBO.setAddCustomer(Boolean.TRUE);
        customerBO.setCustomerNature(CustomerNatureEnum.END_USRE_GOVERNMENT);
        customerBO.setHeatingArea(houseArea);
        customerBO.setBuildingType(buildingType);
        customerBO.setFloorSpace(houseArea);
        customerBO.setCommonHeatingArea(houseArea);
        customerBO.setHeatingMode(heatingMode);
        customerBO.setEnergyType(EnergyTypeEnum.getEnum(String.valueOf(energyType)));
        if (0 == energyType) { // 能源类型为改电
            customerBO.setElectricityMeterNumber(meterNumber);
        }
        if (1 == energyType) { // 能源类型为改气
            customerBO.setGasMeterNumber(meterNumber);
        }
        if (ObjectUtil.isNull(isVillage)) {
            customerBO.setIsVillage(null);
        } else if ("1".equals(isVillage)) {
            customerBO.setIsVillage(true);
        } else if ("0".equals(isVillage)) {
            customerBO.setIsVillage(false);
        }
        customerBO.setEnergyUsePropertyCode(natureEnergyType);
        customerBO.setIsVillagers(Boolean.valueOf(isVillagers));
        customerBO.setCustomerNameNow(customerNameNow);
        customerBO.setPhoneNow(phoneNow);
        customerBO.setIsHouseholder(isHouseholder);
        customerBO.setIdCardNumberNow(idCardNumberNow);
        customerBO.setCustomerStaffId(customerStaffId);
        return customerBO;
    }

}
